#!/usr/bin/env python

import os
import subprocess
import socket
import sys
import time
from struct import *

import paho.mqtt.client as mqtt

sent_mid = -1

def on_connect(mqttc, obj, flags, rc):
    global sent_mid
    if rc != 0:
        exit(rc)
    else:
        (res, sent_mid) = mqttc.publish("pub/qos0/no-payload/test")

def on_publish(mqttc, obj, mid):
    global sent_mid, run
    if sent_mid == mid:
        mqttc.disconnect()
        run = 0

run = -1
mqttc = mqtt.Client("publish-qos0-test-np", run)
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish

mqttc.connect("localhost", 1888)
while run == -1:
    mqttc.loop()

exit(run)
